﻿Imports System.Xml.Serialization

Namespace Commands
    Public Class RequestFilePart
        Inherits Framework.Net.CommandBase

        Private Const mRequestCode As String = "RequestFilePart"
        Private Const mReplyCode As String = "RequestFilePartReply"

        Public Sub New()
            MyBase.New(mRequestCode, mReplyCode)
        End Sub

        Public Class Request
            Public FileSha512 As String
            Public FileSize As Long
            Public PartId As Integer
            Public TrackingId As String
        End Class

        Friend Shared Function SendRequest(ByVal node As Framework.Net.Node, ByVal fileSha512 As String, ByVal fileSize As Long, ByVal partId As Integer, ByVal trackingId As String) As Integer
            Dim clsRequest As New Request
            clsRequest.FileSha512 = fileSha512
            clsRequest.FileSize = fileSize
            clsRequest.PartId = partId
            'clsRequest.Tag = tag
            clsRequest.TrackingId = TrackingId

            Return node.SendCommandMessageAsync(mRequestCode, Framework.Serialization.ToXmlBytes(clsRequest))
        End Function

        Public Overrides Sub HandleRequest(ByVal envelope As Framework.Net.MessageEnvelope)
            Dim clsRequest As Request = Framework.Serialization.FromXmlBytes(Of Request)(envelope.Message.Message)

            Dim clsFile As Entities.SharedItemFile
            clsFile = EntityHandlers.SharedItemsFiles.GetBySHA512HashAndSizeNotOrphaned(clsRequest.FileSha512, clsRequest.FileSize)
            If envelope.Node.BuddyKeyMaster.IsGranted(clsFile) Then
                Dim clsPart As New TransferEngine.TransmitPartWorkItem
                clsPart.PartId = clsRequest.PartId
                clsPart.node = envelope.Node
                clsPart.FilePath = System.IO.Path.Combine(clsFile.Directory, clsFile.FileName)
                clsPart.InReplyToMessageId = envelope.Message.Id
                clsPart.TrackingId = clsRequest.TrackingId

                TransferEngine.Manager.QueueUploadPartRequest(clsPart)
            ElseIf envelope.Node.BuddyKeyMaster.IsGranted(Settings.IncomingDownloadsGrantedToSecurityGroupIds) Then
                'we didn't find it among the shared files. Maybe it's in the downloading directories
                Dim clsPart As New TransferEngine.TransmitPartWorkItem

                Dim clsDLFile As Entities.Downloads.File
                clsDLFile = EntityHandlers.DownloadsFiles.GetBySHA512HashAndSize(clsRequest.FileSha512, clsRequest.FileSize)
                Dim udtPartStatus As Entities.Downloads.PartStatus = Entities.Downloads.PartStatus.Missing
                If clsDLFile IsNot Nothing Then
                    udtPartStatus = CType(clsDLFile.PartStatuses(clsRequest.PartId), Entities.Downloads.PartStatus)
                End If
                clsPart.PartId = clsRequest.PartId
                clsPart.Node = envelope.Node
                clsPart.TrackingId = clsRequest.TrackingId
                clsPart.InReplyToMessageId = envelope.Message.Id
                If udtPartStatus = Entities.Downloads.PartStatus.Complete Then
                    clsPart.FilePath = System.IO.Path.Combine(clsDLFile.Directory, clsDLFile.Name)
                Else
                    clsPart.FilePath = Nothing
                End If
                TransferEngine.Manager.QueueUploadPartRequest(clsPart)
            End If
        End Sub

        Public Overrides Sub HandleReply(ByVal envelope As Framework.Net.MessageEnvelope)
            Throw New NotImplementedException
        End Sub
    End Class



End Namespace


